<template>
  <div>
    <header class="app-header">
      <div @click="back" class="backRoute">
        <i class="iconfont icon-arrow-left-bold"></i>
      </div>
      收藏列表</header>
    <!-- <div class="scroll-content">
      <div class="scroll-item" v-for="(item, index) in collect" :key="index">
        <div class="studys">
          <div>{{item.title}}</div>
        </div>
      </div>
    </div> -->
    <div class="core-container">
      <div class="scroll-wrapper" ref="scroll">
        <div class="scroll-content">
          <div class="scroll-item" data-type="0" v-for="(item, index) in collect" :key="index" @click="clickPush(item)">
            <div class="studys" @touchstart.capture="touchStart" @touchend.capture="touchEnd">
              {{item.title}}
            </div>
            <div class="del">取消收藏</div>
          </div>

        </div>
      </div>
    </div>
    <!-- {{collections}} {{collect}} -->
  </div>
</template>

<script>
import { mapActions, mapState } from 'vuex'
export default {
  data () {
    return {
      startX: 0,
      endX: 0,
    }
  },
  methods: {
    checkSlide () {
      let listItems = document.querySelectorAll('.scroll-item');
      for (let i = 0; i < listItems.length; i++) {
        if (listItems[i].dataset.type == 1) {
          return true;
        }
      }
      return false;
    },
    touchStart (e) {
      // console.log(e)
      this.startX = e.touches[0].clientX;
    },
    touchEnd (e) {
      // console.log(e)
      let parentElement = e.currentTarget.parentElement;
      // 记录结束位置
      this.endX = e.changedTouches[0].clientX;
      // 左滑
      if (parentElement.dataset.type == 0 && this.startX - this.endX > 30) {
        // console.log()
        // this.$refs.a[0].style.overflow=''
        this.restSlide();
        parentElement.dataset.type = 1;
      }
      // 右滑
      if (parentElement.dataset.type == 1 && this.startX - this.endX < -30) {
        this.restSlide();
        parentElement.dataset.type = 0;
      }
      this.startX = 0;
      this.endX = 0;
    },
    restSlide () {
      let listItems = document.querySelectorAll('.scroll-item');
      // 复位
      for (let i = 0; i < listItems.length; i++) {
        listItems[i].dataset.type = 0;
      }
    },
    ...mapActions('login', ['getCollection', 'getMsbdByCollect']),
    back () {
      this.$router.back()
    },
    async clickPush (item) {
      await this.$router.push('/detail/' + item.id)
    }
  },
  async mounted () {
    this.getCollection({ uid: this.form.id })
    await this.getMsbdByCollect({ uid: this.form.id })
  },
  computed: {
    ...mapState('login', ['collections', 'form', 'collect'])
  }
}
</script>

<style scoped>
.backRoute {
  position: absolute;
  left: 17px;
  width: 60px;
}

.core-container .scroll-wrapper {
  height: calc(100vh - 60px);
  overflow: hidden;
}
.core-container .scroll-wrapper .scroll-item {
  height: 50px;
  line-height: 50px;
  font-size: 18px;
  border-bottom: 1px solid #eee;
  text-align: left;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-indent: 10px;
  border-bottom: 1px solid #ccc;
  width: 100%;
  margin: auto;
  position: relative;
}

.core-container .scroll-wrapper .scroll-item .study {
  color: #999999;
}

.core-container .scroll-wrapper .scroll-item {
  background-color: #f3f5f7;
}

.studys {
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
}

.del {
  position: absolute;
  background-color: red;
  width: 70px;
  top: 0;
  right: -70px;
  color: white;
  font-size: 12px;
  text-align: center;
}

.scroll-item[data-type='0'] {
  transform: translate3d(0, 0, 0);
}
.scroll-item[data-type='1'] {
  transform: translate3d(-70px, 0, 0);
}

.scroll-item {
  position: relative;
  height: 1.6rem;
  -webkit-transition: all 0.2s;
  transition: all 0.2s;
}
</style>